import libCookies from '@/lib/cookies';
import libLexicon from '@/lib/lexicon';
import libUtils from '@/lib/utils';
import libWeb from '@/lib/web';
import store from '@/store/index';
import XEUtils from 'xe-utils';
import { I_sysVersion } from '../arg/getSysInfo';
import { I_compBox } from './compBox';

type T_XEUtils = typeof XEUtils;
type T_libUtils = typeof libUtils;
type T_utils = T_XEUtils & T_libUtils;

export class T_base {
  // 类名
  readonly className: string = `T_base`;

  // 方法库
  readonly utils: T_utils = XEUtils.assign(XEUtils, libUtils);

  // 引入xeUtil包方法集
  readonly XEUtils: T_XEUtils = XEUtils;

  // 本地方法集
  readonly libUtils: T_libUtils = libUtils;

  // 词典
  readonly lexicon: typeof libLexicon = libLexicon;

  readonly cookies: typeof libCookies = libCookies;

  // 本地web包
  readonly web: typeof libWeb = libWeb;

  // 本地url词典
  readonly url: typeof libLexicon.url = libLexicon.url;

  readonly store: any = store;

  // 全局变量
  readonly globalVar: typeof libLexicon.globalVar = libLexicon.globalVar;

  // 组件箱
  private _compBox: I_compBox;

  public getUserID(): string {
    return this.store.getters.UserId;
  }

  public getUserNo(): string {
    return this.store.getters.UserNo || 'undefined';
  }

  public getUserName(): string {
    return this.store.getters.UserName || 'undefined';
  }

  public getUserIsGuest(): boolean {
    return this.store.getters.SysInfo.info.currentUserIsGuest || 'undefined';
  }

  public getUserInfo(): any {
    return {
      userID: this.getUserID(),
      userNo: this.getUserNo(),
      userName: this.getUserName(),
      isGuest: this.getUserIsGuest()
    };
  }

  public getDBName(): string {
    return this.store.getters.DBName || 'undefined';
  }

  public getWsCliID(): string {
    return this.store.getters.ConnCliID || 'undefined';
  }

  public getSysDBName(): string {
    return this.store.getters.SysInfo.set.sysDBName || 'undefined';
  }

  public getSysVersion(): I_sysVersion {
    const r: I_sysVersion = JSON.parse(
      this.store.getters.SysInfo.info.systemVersion
    );
    r.web = libLexicon.version;
    return r;
  }

  public getSysDefLang(): string {
    return this.store.getters.SysInfo.info.defaultLang.toString();
  }

  public getSnowWorkID(): bigint {
    const workID: string = this.store.getters.SysInfo.set.snowWorkID;
    if (!workID) {
      return -1n;
    }
    return BigInt(workID);
  }

  public getSnowIDSeed(): bigint {
    const seed: string =
      this.store.getters.SysInfo.set.snowIDSeed || 'undefined';
    if (!seed) {
      return -1n;
    }
    return BigInt(seed);
  }

  public getStore(): any {
    return this.store;
  }

  /**
   * @Author: wiz
   * @param { obj vue 对象,ref ref名 }
   * @return {*}
   * @description:全局查找ref
   */
  // 查找Ref
  public lookupRefs(obj: any, ref: string): any {
    return this.lookupRefsDown(obj, ref) || this.lookupRefsUp(obj, ref) || null;
  }
  /**
   * @Author: wiz
   * @param {any} obj
   * @param {string} ref
   * @return {*}
   * @description: 向上查找ref
   */

  public lookupRefsUp(obj: any, ref: string): any {
    return this.lookup(`up`, obj, ref);
  }
  /**
   * @Author: wiz
   * @param {any} obj
   * @param {string} ref
   * @return {*}
   * @description:向下查找ref
   */

  public lookupRefsDown(obj: any, ref: string): any {
    return this.lookup(`down`, obj, ref);
  }

  /**
   * @Author: wiz
   * @param {string} s
   * @param {any} obj
   * @param {string} ref
   * @return {*}
   * @description: 查找主函数
   */

  private lookup(s: string, obj: any, ref: string): any {
    if (obj && obj.$refs && obj.$refs[ref]) {
      return obj.$refs[ref];
    }
    if (s === `up` && obj) {
      this.lookup(s, obj.$parent, ref);
    } else if (s === `down` && obj) {
      if (obj.$children && obj.$children.length > 0) {
        for (const item of obj.$children) {
          if (!item) {
            continue;
          }
          const _obj = this.lookup(s, item, ref);
          if (_obj) {
            return _obj;
          }
        }
      } else {
        return null;
      }
    } else {
      return null;
    }
    return null;
  }
  /**
   * @Author: wiz
   * @param {*}
   * @return {*}
   * @description: 取组件箱
   */

  public get compBox(): I_compBox {
    return this._compBox;
  }
  /**
   * @Author: wiz
   * @param {I_compBox} cb
   * @return {*}
   * @description: 设置组件箱
   */

  public set compBox(cb: I_compBox) {
    this._compBox = cb;
  }
}
export interface I_base extends T_base {}
